// 顶部：仅类型导入 + 拆分 logger 的值/类型导入
import Logger from "../../logger";
import type { ILogger } from "../../logger";
import { LoggerLevel } from "../../logger";
import type { IUtilsFormatters } from "./formatters";

export type { IUtilsFormatters };

/**
 * 工具 formatters
 */
class UtilsFormatters implements IUtilsFormatters {
    private logger: ILogger;

    constructor(loggerLevel?: LoggerLevel) {
        this.logger = new Logger('UtilsFormatters', loggerLevel);
    }

    /**
     * 格式化金额
     * @param amount 金额数值
     * @param currency 货币符号，默认为 '¥'
     * @returns 格式化后的金额字符串
     */
    formatCurrency(amount: number, currency: string = '¥'): string {
        if (typeof amount !== 'number' || isNaN(amount)) {
            this.logger.warn('formatCurrency()', '传入的金额不是有效数字');
            return `${currency}0.00`;
        }
        return `${currency}${amount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}`;
    }

    /**
     * 格式化日期
     * @param date 日期对象
     * @param format 日期格式，默认为 'YYYY-MM-DD'
     * @returns 格式化后的日期字符串
     */
    formatDate(date: Date, format: string = 'YYYY-MM-DD'): string {
        if (!(date instanceof Date) || isNaN(date.getTime())) {
            this.logger.warn('formatDate()', '传入的日期不是有效的Date对象');
            return '';
        }

        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');

        return format
            .replace('YYYY', String(year))
            .replace('MM', month)
            .replace('DD', day)
            .replace('HH', hours)
            .replace('mm', minutes)
            .replace('ss', seconds);
    }

    /**
     * 格式化文件大小
     * @param bytes 字节数
     * @returns 格式化后的文件大小字符串
     */
    formatFileSize(bytes: number): string {
        if (typeof bytes !== 'number' || bytes < 0) {
            this.logger.warn('formatFileSize()', '传入的字节数不是有效数字');
            return '0 Bytes';
        }

        if (bytes === 0) return '0 Bytes';
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    }

    /**
     * 格式化手机号
     * @param phone 手机号字符串
     * @returns 格式化后的手机号字符串
     */
    formatPhoneNumber(phone: string): string {
        if (typeof phone !== 'string') {
            this.logger.warn('formatPhoneNumber()', '传入的手机号不是字符串');
            return '';
        }

        // 移除所有非数字字符
        const cleanPhone = phone.replace(/\D/g, '');
        
        // 检查是否是11位手机号
        if (cleanPhone.length !== 11) {
            this.logger.warn('formatPhoneNumber()', '手机号长度不正确');
            return phone;
        }

        return cleanPhone.replace(/(\d{3})(\d{4})(\d{4})/, '$1 $2 $3');
    }
}

export default new UtilsFormatters() as IUtilsFormatters;